package com.broada.apm.dao.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.broada.apm.dao.RelationDao;
import com.broada.apm.model.ProtocolRelation;
import com.broada.apm.model.Relation;
import com.broada.apm.model.Relations;
import com.broada.apm.model.Service;
import com.broada.apm.model.TopologyRelation;

public class MysqlRelationDao extends MysqlAbstractDao implements RelationDao {

	public static final String RELATION_NAMESPACE = "com.broada.apm.dao.RelationDao."; 
	
	@Override
	public void addRelation(Relation relation) {
		getSqlSession().insert(RELATION_NAMESPACE + "addRelation", relation);
	}

	@Override
	public void deleteRelation(Relation relation) {
		getSqlSession().delete(RELATION_NAMESPACE + "deleteRelation", relation);
	}
	
	@Override
	public void deleteAllDisRelation() {
		getSqlSession().delete(RELATION_NAMESPACE + "deleteAllDisRelation");
	}

	@Override
	public Relation getRelationById(String id) {
		return getSqlSession().selectOne(RELATION_NAMESPACE + "getRelationById", id);
	}

	@Override
	public List<Relation> getRelationByAppId(String appId) {
		return getSqlSession().selectList(RELATION_NAMESPACE + "getRelationByAppId", appId);
	}

	@Override
	public void deleteRelationByAppId(String appId) {
		getSqlSession().delete(RELATION_NAMESPACE + "deleteRelationByAppId", appId);
	}

	@Override
	public List<Relation> getRelationByFromOrTo(String[] serviceIds) {
		return getSqlSession().selectList(RELATION_NAMESPACE + "getRelationByFromOrTo", serviceIds);
	}

	@Override
	public void deleteRelationByFromOrTo(String[] serviceIds) {
		getSqlSession().delete(RELATION_NAMESPACE + "deleteRelationByFromOrTo", serviceIds);
	}

	@Override
	public List<Relation> getRelationByFromAndTo(String[] fromSerIds,
			String[] toSerIds) {
		Map<String, String[]> paramMap = new HashMap<String, String[]>();
		paramMap.put("fromSerIds", fromSerIds);
		paramMap.put("toSerIds", toSerIds);
		return getSqlSession().selectList(RELATION_NAMESPACE + "getRelationByFromAndTo", paramMap);
	}

	@Override
	public List<ProtocolRelation> getDisRelationBySrcIp(Long srcIp) {
		return getSqlSession().selectList(RELATION_NAMESPACE + "getDisRelationBySrcIp", srcIp);
	}

	@Override
	public List<ProtocolRelation> getDisRelationByServiceId(String serviceId) {
		return getSqlSession().selectList(RELATION_NAMESPACE + "getDisRelationByServiceId", serviceId);
	}

	@Override
	public ProtocolRelation getDisRelationById(String relationId) {
		return getSqlSession().selectOne(RELATION_NAMESPACE + "getDisRelationById", relationId);
	}

	@Override
	public void addDisRelation(ProtocolRelation relation) {
		getSqlSession().insert(RELATION_NAMESPACE + "addDisRelation", relation);
	}

	@Override
	public void deleteDisRlation(ProtocolRelation relation) {
		getSqlSession().delete(RELATION_NAMESPACE + "deleteDisRelation", relation);
		
	}

	@Override
	public List<ProtocolRelation> getDisRelationByService(Service service) {
		return getSqlSession().selectList(RELATION_NAMESPACE + "getDisRelationByService", service);
	}

	@Override
	public List<ProtocolRelation> getAllDisRelation() {
		return getSqlSession().selectList(RELATION_NAMESPACE + "getAllDisRelation");
	}
	
	@Override
	public List<Relations> getAllDisRelationTopology() {
		return getSqlSession().selectList(RELATION_NAMESPACE + "getAllDisRelationTopology");
	}

	@Override
	public List<TopologyRelation> getRelationTopology() {
		return getSqlSession().selectList(RELATION_NAMESPACE + "getRelationTopology");
	}
	
	@Override
	public void deleteValidRelation() {
		getSqlSession().delete(RELATION_NAMESPACE + "deleteValidRelation");
	}

}
